<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="参数别名" prop="paramAlias">
        <el-input
          v-model="queryParams.paramAlias"
          placeholder="请输入参数别名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参数编码" prop="paramCode">
        <el-input
          v-model="queryParams.paramCode"
          placeholder="请输入参数编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参数名称" prop="paramName">
        <el-input
          v-model="queryParams.paramName"
          placeholder="请输入参数名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="算法类型" prop="algorithmType">
        <el-select v-model="queryParams.algorithmType" placeholder="请选择算法类型" clearable>
          <el-option
            v-for="dict in dict.type.algorithm_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="descList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50"/>
      <el-table-column label="参数别名" align="center" prop="paramAlias" width="130" show-overflow-tooltip />
      <el-table-column label="参数编码" align="center" prop="paramCode" width="130" show-overflow-tooltip />
      <el-table-column label="参数名称" align="center" prop="paramName" width="130" show-overflow-tooltip />
      <el-table-column label="数据类型" align="center" prop="dataType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.param_data_type" :value="scope.row.dataType"/>
        </template>
      </el-table-column>
      <el-table-column label="取值范围" align="center" prop="valueRange" />
      <el-table-column label="可选性" align="center" prop="selectivity" />
      <el-table-column label="参数解释" align="center" prop="paramExplain" width="240" show-overflow-tooltip />
      <el-table-column label="算法类型" align="center" prop="algorithmType" width="130" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.algorithm_type" :value="scope.row.algorithmType"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改算法参数说明对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="88px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="参数别名" prop="paramAlias">
              <el-input v-model="form.paramAlias" placeholder="请输入参数别名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="参数编码" prop="paramCode">
              <el-input v-model="form.paramCode" placeholder="请输入参数编码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="参数名称" prop="paramName">
              <el-input v-model="form.paramName" placeholder="请输入参数名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据类型" prop="dataType">
              <el-select v-model="form.dataType" placeholder="请选择数据类型" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.param_data_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="取值范围" prop="valueRange">
              <el-input v-model="form.valueRange" placeholder="请输入取值范围" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="可选性" prop="selectivity">
              <el-radio-group v-model="form.selectivity">
                <el-radio :label="'是'">是</el-radio>
                <el-radio :label="'否'">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="参数解释" prop="paramExplain">
              <el-input v-model="form.paramExplain" type="textarea" placeholder="请输入内容" maxlength="255" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="默认值" prop="defaultValue">
              <el-input v-model="form.defaultValue" placeholder="请输入默认值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="算法类型" prop="algorithmType">
              <el-select v-model="form.algorithmType" placeholder="请选择算法类型" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.algorithm_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="255" show-word-limit />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDesc, getDesc, delDesc, addDesc, updateDesc } from "@/api/knowledge/desc";

export default {
  name: "Desc",
  dicts: ['module_type', 'param_data_type', 'algorithm_type', 'sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 算法参数说明表格数据
      descList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        paramAlias: null,
        paramCode: null,
        paramName: null,
        dataType: null,
        valueRange: null,
        paramExplain: null,
        selectivity: null,
        defaultValue: null,
        moduleType: null,
        algorithmType: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        paramAlias: [
          { required: true, message: "参数别名不能为空", trigger: "blur" }
        ],
        paramCode: [
          { required: true, message: "参数编码不能为空", trigger: "blur" }
        ],
        paramName: [
          { required: true, message: "参数名称不能为空", trigger: "blur" }
        ],
        paramExplain: [
          { required: true, message: "参数解释不能为空", trigger: "blur" }
        ],
        dataType: [
          { required: true, message: "数据类型不能为空", trigger: "change" }
        ],
        algorithmType: [
          { required: true, message: '算法类型不能为空', trigger: 'change' }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询算法参数说明列表 */
    getList() {
      this.loading = true;
      this.queryParams.moduleType = sessionStorage.getItem('moduleType')
      listDesc(this.queryParams).then(response => {
        this.descList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        descId: null,
        paramAlias: null,
        paramCode: null,
        paramName: null,
        dataType: null,
        valueRange: null,
        paramExplain: null,
        selectivity: '是',
        defaultValue: null,
        moduleType: null,
        algorithmType: null,
        delFlag: null,
        status: '0',
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.form.moduleType = sessionStorage.getItem('moduleType')
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.descId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加算法参数说明";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const descId = row.descId || this.ids
      getDesc(descId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改算法参数说明";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.descId != null) {
            updateDesc(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDesc(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const descIds = row.descId || this.ids;
      this.$modal.confirm('是否确认删除算法参数说明编号为"' + descIds + '"的数据项？').then(function() {
        return delDesc(descIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('knowledge/desc/export', {
        ...this.queryParams
      }, `desc_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
